package com.example.testdemo.demos.web.Service.Impl;

import com.example.testdemo.demos.web.Config.Result;
import com.example.testdemo.demos.web.Config.ResultEnum;
import com.example.testdemo.demos.web.Service.UploadService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;


@Service
public class UploadServiceImpl implements UploadService {

    @Value("${file.filePath}")
    private String filePath;
    @Value("${file.uploadPath}")
    private String uploadPath;
    @Value("${file.downloadPath}")
    private String downloadPath;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @Override
    public Result fileUpload(MultipartFile file){
        Result r = new Result();
        String Rrl= filePath;//服务器访问前缀
        String filePath =uploadPath; //服务器的存放路径
        if (file.isEmpty()){
            r.setCode(ResultEnum.BADREQUEST.getCode());
            r.setMsg("文件上传失败！");
        }else {
            //取出文件
            String OriginalFilename=file.getOriginalFilename();
           //文件重命名
            String fileName =System.currentTimeMillis()+"."+OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1);
            //拼接文件存放地址
            File dest=new File(filePath+fileName);
            //判断文件路径是否存在  如果不存在就创建文件路径
            if (dest.getParentFile().exists())
                dest.getParentFile().mkdirs();
            try {
                HashMap<String,String> map =new HashMap<>();
                //文件保存
                file.transferTo(dest);
                map.put("filePath",Rrl+fileName);
                map.put("fileName",fileName);
                r.setData(map);
                //服务器地址路径 + 文件名字
                //r.setData( fileName);//本地
                r.setCode(ResultEnum.SUCCESS.getCode());
                r.setMsg("上传成功");

                //抛出异常
            } catch (IOException e) {e.printStackTrace();r.setData(e);}
        }
        return r;
    }

    /**
     * 文件删除
     * @param fileName
     * @return
     */
    @Override
    public Result fileDelete(String fileName){
        //String []filenameindex = fileName.split(filePath);
        //String imgname = null;
        //for(String listName : filenameindex){imgname=listName;}
       // File file = new File(uploadPath+imgname);
        File file = new File(uploadPath+fileName);
        Result r = new Result();
        if(!file.exists()){
            r.setData("删除文件失败："+fileName+"不存在！");
            r.setCode(ResultEnum.SUCCESS.getCode());
            r.setMsg("删除失败");
        }else{
            //判断这是不是一个文件，ps：有可能是文件夹
            if(file.isFile()){
                file.delete();//删除
                r.setCode(ResultEnum.SUCCESS.getCode());
                r.setMsg("删除成功");
            }else {
                r.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                r.setMsg("不能删除文件夹！");
            }
        }

        return  r;
    }

}
